package com.happy.study.config;

import com.happy.study.entity.UserEntity;
import com.happy.study.util.StateEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class StudyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserEntity userEntity = (UserEntity) request.getSession ().getAttribute ( "userEntity" );
        if ( userEntity == null ) {
            request.setAttribute ( "code", StateEnum.REQUEST_POWER_EXCE.getCode () );
            request.getRequestDispatcher ( "/index" ).forward ( request, response );
            return false;
        }
        return true;
    }

    @Override
    public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
